<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams">
      <el-form-item label="样本组名称" prop="sampGroupName">
        <el-input
          v-model="queryParams.sampGroupName"
          clearable
          placeholder="请输入样本组名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="样本组描述" prop="sampGroupDesc">
        <el-input
          v-model="queryParams.sampGroupDesc"
          clearable
          placeholder="请输入样本组描述"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['biobank:sampleGroup:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="sampleGroupList"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      default-expand-all
      row-key="sampGroupId"
    >
      <el-table-column align="center" label="样本组编码" prop="sampGroupCode"/>
      <el-table-column align="center" label="样本组名称" prop="sampGroupName"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="样本组描述" prop="sampGroupDesc"/>
      <el-table-column align="center" label="同层级排序" prop="sampGroupSort"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['biobank:sampleGroup:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['biobank:sampleGroup:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改样本组 对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="父样本组" prop="parentId">
          <treeselect v-model="form.parentId" :normalizer="normalizer" :options="sampleGroupOptions"
                      placeholder="请选择父样本组ID"
          />
        </el-form-item>
        <el-form-item label="样本组编码" prop="sampGroupCode">
          <el-input v-model="form.sampGroupCode" placeholder="请输入样本组编码"/>
        </el-form-item>
        <el-form-item label="样本组名称" prop="sampGroupName">
          <el-input v-model="form.sampGroupName" placeholder="请输入样本组名称"/>
        </el-form-item>
        <el-form-item label="样本组描述" prop="sampGroupDesc">
          <el-input v-model="form.sampGroupDesc" placeholder="请输入样本组描述"/>
        </el-form-item>
        <el-form-item label="同层级排序" prop="sampGroupSort">
          <el-input-number v-model="form.sampGroupSort" :min="1" controls-position="right" placeholder="请输入样本组同层级排序"
                           style="width: 100%"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addSampleGroup,
  delSampleGroup,
  getSampleGroup,
  listSampleGroup,
  updateSampleGroup
} from '@/api/biobank/sampleGroup'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'SampleGroup',
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 样本组 表格数据
      sampleGroupList: [],
      // 样本组 树选项
      sampleGroupOptions: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        sampGroupName: null,
        sampGroupDesc: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: { required: true, trigger: 'change', message: '父样本组不能为空' },
        sampGroupCode: [{ required: true, trigger: 'blur', message: '样本组编码不能为空' }, {
          max: 50,
          trigger: 'blur',
          message: '样本组编码不能超过50个字符'
        }],
        sampGroupName: [{ required: true, trigger: 'blur', message: '样本组名称不能为空' }, {
          max: 50,
          trigger: 'blur',
          message: '样本组名称不能超过50个字符'
        }],
        sampGroupDesc: { max: 100, trigger: 'blur', message: '样本组描述不能超过100个字符' }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询样本组 列表 */
    getList() {
      this.loading = true
      listSampleGroup(this.queryParams).then(response => {
        this.sampleGroupList = this.handleTree(response.data, 'sampGroupId', 'parentId')
        this.loading = false
      })
    },
    /** 转换样本组 数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.sampGroupId,
        label: node.sampGroupName,
        children: node.children
      }
    },
    /** 查询样本组 下拉树结构 */
    getTreeselect() {
      listSampleGroup().then(response => {
        this.sampleGroupOptions = []
        const data = { sampGroupId: 0, sampGroupName: '顶级节点', children: [] }
        data.children = this.handleTree(response.data, 'sampGroupId', 'parentId')
        this.sampleGroupOptions.push(data)
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        sampGroupId: null,
        parentId: null,
        sampGroupCode: null,
        sampGroupName: null,
        sampGroupDesc: null,
        sampGroupSort: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getTreeselect()
      this.open = true
      this.title = '添加样本组 '
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getTreeselect()
      if (row != null) {
        this.form.parentId = row.sampGroupId
      }
      getSampleGroup(row.sampGroupId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改样本组 '
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.sampGroupId != null) {
            updateSampleGroup(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSampleGroup(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除样本组 编号为"' + row.sampGroupId + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delSampleGroup(row.sampGroupId)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>
